package striveen.util.used.view.view;

import android.content.Context;
import aym.util.log.Log;
import aym.view.adddata.LoadingDataDialog;
import com.example.aymutilused2.R;

/**
 * Created by admin on 15/6/26.
 */
public class MyLoadingDataDialogManager {
    private final String TAG = MyLoadingDataDialogManager.class.getSimpleName();
    private int index = 0;
    private LoadingDataDialog progressDialog;
    private boolean isCancelable=true;
    private MyLoadingDataDialogManager(Context context, String loadingMsg) {
        this.progressDialog = new LoadingDataDialog(context);
        if(loadingMsg == null) {
            this.progressDialog.setMessage(context.getString(R.string.text_loadingdata));
        }

        this.progressDialog.setCancelable(isCancelable);
        this.progressDialog.setCanceledOnTouchOutside(false);
    }

    public boolean isCancelable() {
        return isCancelable;
    }

    public void setIsCancelable(boolean isCancelable) {
        this.isCancelable = isCancelable;
    }

    public static MyLoadingDataDialogManager init(Context context) {
        return new MyLoadingDataDialogManager(context, (String)null);
    }

    public static MyLoadingDataDialogManager init(Context context, String loadingMsg) {
        return new MyLoadingDataDialogManager(context, loadingMsg);
    }

    public static MyLoadingDataDialogManager init(Context context, int resIdLoadingMsg) {
        return new MyLoadingDataDialogManager(context, context.getResources().getString(resIdLoadingMsg));
    }

    public void destroy() {
        if(this.progressDialog != null) {
            this.progressDialog.dismiss();
            this.progressDialog = null;
        }

    }

    public void show() {
        if(this.index == 0) {
            if(this.progressDialog != null) {
                this.progressDialog.show();
            } else {
                Log.w(this.TAG, "progressDialog not init");
            }
        }

        ++this.index;
    }

    public void update(String msg) {
        if(this.progressDialog != null) {
            this.progressDialog.setMessage(msg);
        } else {
            Log.w(this.TAG, "progressDialog not init");
        }
    }
    public void update(int resId) {
        if(this.progressDialog != null) {
            this.progressDialog.setMessage(resId);
        } else {
            Log.w(this.TAG, "progressDialog not init");
        }
    }
    public void dismiss() {
        if(this.index <= 1) {
            if(this.progressDialog != null) {
                this.progressDialog.hide();
            } else {
                Log.w(this.TAG, "progressDialog not init");
            }

            this.index = 0;
        } else {
            --this.index;
        }

    }
}

